package com.qire.manhua.dal.service;

import com.lzy.okgo.db.CacheManager;
import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.CustomEvent;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.common.model.entity.UserEntity;
import com.qire.common.utils.NoticeEventBus;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.dal.IUserDataDAL;
import com.qire.manhua.model.enumeration.AccountBusinessPanelLayout;
import com.qire.manhua.model.viewModel.impl.AccountCancellationGuideViewModel;
import com.qire.manhua.model.vo.AccountCancellationCheckInfoVO;
import com.qire.manhua.model.vo.AccountCancellationCheckInfoVO.MallOrderCheckVO;
import com.qire.manhua.model.vo.AccountCancellationResultsVO;
import com.qire.manhua.model.vo.AccountCancellationResultsVO.CheckData;
import com.qire.manhua.model.vo.MobileAuthInfoVO;
import com.qire.manhua.model.vo.OkResponseVO;

/**
 * 账户销户引导页数据访问服务
 */
public class AccountCancellationGuideService extends DataService {

    private final IUserDataDAL userDataDAL = WarehouseFactory.create(this, IUserDataDAL.class);
    private final AccountCancellationGuideViewModel viewModel;

    public AccountCancellationGuideService(AccountCancellationGuideViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**
     * 查询账户销户人工审核结果
     */
    public void accountCancellationQuery() {
        userDataDAL.accountCancellationQuery();
    }
    @StrategyAnnotation(event = Url.ArtificialCancellationQuery)
    public void accountCancellationQueryCallback(AccountCancellationResultsVO dataVO, boolean isCache) {
        if(dataVO.checkState == -1) { //未申请
            viewModel.initPanelLayout();
            return;
        }
        if(dataVO.checkState == 0) { //未审核
            CheckData resultsVO = dataVO.checkData;
            viewModel.showArtificialResults(resultsVO.createTime, resultsVO.contactInfo, resultsVO.reserveTime, resultsVO.reason);
            viewModel.gotoPage(AccountBusinessPanelLayout.AccountCancellationWaiting);
            return;
        }
        //通过了
        AccountBusinessPanelLayout.AccountCancellationNotice.addNext(AccountBusinessPanelLayout.AccountCancellationGuideFinishHint);
        AccountBusinessPanelLayout.AccountCancellationArtificialAppealsPass.addNext(AccountBusinessPanelLayout.AccountCancellationNotice);
        viewModel.gotoPage(AccountBusinessPanelLayout.AccountCancellationArtificialAppealsPass);
    }

    /**
     * 验证手机号码合法性
     * @param phoneNumber
     */
    public void authPhoneNumberBy(String phoneNumber) {
        userDataDAL.fetchMobileAuthInfo(phoneNumber);
    }
    @StrategyAnnotation(event = Url.v5_mobileAuthInfo)
    public void fetchMobileAuthInfoCallback(MobileAuthInfoVO mobileAuthInfoVO, boolean isCache) {
        if(ObjectUtil.isNull(mobileAuthInfoVO)) {
            return;
        }
        //手机身份信息验证API调用后回调处理：验证手机是否已注册等信息,确定其打开的登录框页面顺序
        if(mobileAuthInfoVO.is_reg != 1) {
            ToastHelper.showToast("手机号未注册");
            return;
        }
        if(mobileAuthInfoVO.user_id != UserEntity.self.getId()) {
            ToastHelper.showToast("手机号不匹配");
            return;
        }
        viewModel.nextPage();
    }

    /**
     * 获取短信验证码并发送到指定手机号码
     * @param phoneNumber
     */
    public void fetchVerificationCode(String phoneNumber) {
        userDataDAL.fetchVerificationCode(phoneNumber);
    }
    @StrategyAnnotation(event = Url.FetchVerificationCode)
    public void fetchVerificationCodeCallback(OkResponseVO dataVO, boolean isCache) {
        ToastHelper.showToast("验证码已发送请注意查收！");
    }

    /**
     * 校验手机短信验证码
     * @param phoneNumber 接受验证码手机号
     * @param verifyCode 短信验证码
     */
    public void checkVerificationCode(String phoneNumber, String verifyCode) {
        userDataDAL.checkVerificationCode(phoneNumber, verifyCode);
    }
    @StrategyAnnotation(event = Url.CheckVerificationCode)
    public void checkVerificationCodeCallback(OkResponseVO dataVO, boolean isCache) {
        ToastHelper.showSnackBar("验证成功！", Prompt.SUCCESS);
        viewModel.nextPage();
    }

    /**
     * 注销账户前核对用户数据，是否存在消费分歧等数据
     */
    public void checkBeforeCancellationAccount() {
        userDataDAL.checkBeforeCancellationAccount();
    }
    @StrategyAnnotation(event = Url.CheckBeforeCancellationAccount)
    public void checkBeforeCancellationAccountCallback(AccountCancellationCheckInfoVO infoVO, boolean isCache) {
        final MallOrderCheckVO failedVO = infoVO.mallOrderCheck;
        if(ObjectUtil.nonNull(failedVO)) {
            viewModel.showAccountCheckFailedInfoDialog(failedVO.tipsText, failedVO.tipsBtnText, failedVO.actTitle, failedVO.actUrl);
        } else {
            viewModel.showAccountCheckSuccessInfoDialog(infoVO.checkTips);
        }
    }

    /**
     * 注销账户
     */
    public void accountCancellation() {
        userDataDAL.accountCancellation();
    }
    @StrategyAnnotation(event = Url.CancellationAccount)
    public void accountCancellationCallback(OkResponseVO dataVO, boolean isCache) {
        viewModel.nextPage();
        UserEntity.self.logout();
        CacheManager.getInstance().clear();
        NoticeEventBus.NOTICE.sendEvent(CustomEvent.LOGOUT);
    }

    /**
     * 注销账户通过人工服务
     * @param phoneNumber 账户电话号码
     * @param contactPhone 联系方式
     * @param appointmentTime 预约联系时间
     * @param reason 原因
     */
    public void accountCancellationByArtificial(String phoneNumber, String contactPhone, String appointmentTime, String reason) {
        userDataDAL.accountCancellationByArtificial(phoneNumber, contactPhone, appointmentTime, reason);
    }
    @StrategyAnnotation(event = Url.ArtificialCancellationAccount)
    public void accountCancellationByArtificialCallback(AccountCancellationResultsVO dataVO, boolean isCache) {
        CheckData resultsVO = dataVO.checkData;
        if(ObjectUtil.nonNull(resultsVO)) {
            viewModel.showArtificialResults(resultsVO.createTime, resultsVO.contactInfo, resultsVO.reserveTime, resultsVO.reason);
        }
        viewModel.nextPage();
    }

}
